package gui.win.genotype;

import core.utils.Utils;
import gui.Segl1;
import gui.utils.Borders;
import gui.utils.Para;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Genotype extends JFrame implements ActionListener, DocumentListener{
    private JButton [] cButtons;
    private JButton [] aaButtons = new JButton[Para.AAS.length];
    private JTextField text;
    private CardLayout layout;
    private JPanel cPanelInner, theCodonButtons;;
    
    public Genotype(int noOfCodons, Segl1 segL1){
        super();
        this.setLocation(segL1.getLocationOnScreen().x + 50, segL1.getLocationOnScreen().y + 50);
        this.setLayout(new FlowLayout());
        this.setSize(new Dimension(400, 546));
        JPanel p = new JPanel();
        p.setBorder(Borders.getTitledBorder(Borders.STANDARD_TITLED_BORDER, "Genotype"));
        p.setLayout(new BorderLayout());


        JPanel p3 = new JPanel(new BorderLayout());
        p3.setBorder(Borders.getTitledBorder(Borders.LIGHT_TITLED_BORDER, "Input sequence"));
        text = new JTextField();
        text.setPreferredSize(new Dimension(360, 20));
        text.setFont(new Font("Tahoma", Font.BOLD, 6));
        text.getDocument().addDocumentListener((DocumentListener)this);
        p3.add(text, BorderLayout.CENTER);

        p.add(p3, BorderLayout.NORTH);


        Para.GENOCODS = null;
        Para.GENOQUERY = null;

        JPanel cPanel = new JPanel(new BorderLayout());
        cPanel.setBorder(Borders.getTitledBorder(Borders.LIGHT_TITLED_BORDER, "If these codon positions"));
        cPanel.setPreferredSize(new Dimension(250,300));
        cPanelInner = new JPanel();
        layout = new CardLayout();
        cPanelInner.setLayout(layout);
        theCodonButtons = new JPanel(new GridLayout(12, 3));
        for(int c=0; c<36; c++){theCodonButtons.add(new JPanel());}
        cPanelInner.add(theCodonButtons, "Table");
        layout.show(cPanelInner, "Table");
        cPanel.add(cPanelInner);

        p.add(cPanel, BorderLayout.WEST);

        JPanel p1 = new JPanel();
        p1.setLayout(new BorderLayout());
        JPanel qPanel = new JPanel();
        qPanel.setBorder(Borders.getTitledBorder(Borders.LIGHT_TITLED_BORDER, "Contain"));
        qPanel.setLayout(new GridLayout(10,2));
        Para.GENOAA = new boolean[Para.AAS.length];
        for(int q=0; q<Para.AAS.length; q++){
            Para.GENOAA[q] = false;
            qPanel.add(Utils.getButton(aaButtons[q] = new JButton(Para.AAS[q]+""), 46, 22, new Color(102, 102, 102), new Color(178, 178, 178), this, Para.AAS[q]+""));
        }
        
        p1.add(qPanel, BorderLayout.NORTH);
        JPanel result = new JPanel();
        result.setBorder(Borders.getTitledBorder(Borders.LIGHT_TITLED_BORDER, "Annotate read as:"));
        result.add(new JLabel("TRUE"), BorderLayout.CENTER);
        p1.add(result, BorderLayout.SOUTH);
        p.add(p1, BorderLayout.CENTER);

        this.add(p, BorderLayout.CENTER);
        this.add(new Buttons(this, segL1), BorderLayout.SOUTH);
        this.setVisible(true);
        this.setAlwaysOnTop(true);
    }

    public void closeFrame(){this.dispose();}

    @Override
    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        for(int c=0; c<Para.GENOAA.length; c++){
            if(source.equals(aaButtons[c])){
                if(Para.GENOAA[c] == false){
                    Para.GENOAA[c] = true;
                    aaButtons[c].setForeground(Color.red);
                }
                else{
                    Para.GENOAA[c] = true;
                    aaButtons[c].setForeground(new Color(102, 102, 102));
                }
                return;
            }
        }
        for(int c=0; c<cButtons.length; c++){
            if(source.equals(cButtons[c])){
                if(Para.GENOCODS[c] == false){
                    Para.GENOCODS[c] = true;
                    cButtons[c].setForeground(Color.red);
                }
                else{
                    Para.GENOCODS[c] = true;
                    cButtons[c].setForeground(new Color(102, 102, 102));
                }
                return;
            }
        }
    }

    static int index = 0;
    private void setUpButtons(){
        if(isInputOK()){
            String codon = "";
            Para.GENOQUERY = text.getText();
            ArrayList <String> inputCodons = new ArrayList();
            for(int x=0; x<Para.GENOQUERY.length(); x++){
                codon += Para.GENOQUERY.charAt(x);
                if(codon.length()==3){
                    inputCodons.add(codon);
                    codon = "";
                }
            }
            Para.GENOCODS = new boolean[inputCodons.size()];
            cButtons = new JButton[inputCodons.size()];
            theCodonButtons = new JPanel(new GridLayout(12, 3));
            for(int c=0; c<inputCodons.size(); c++){
                Para.GENOCODS[c] = false;
                theCodonButtons.add(Utils.getButton(cButtons[c] = new JButton(c+"|"+inputCodons.get(c)+"|"+Utils.trans(inputCodons.get(c))), 76, 22, new Color(102, 102, 102), new Color(178, 178, 178), this, "codon positon (relative to input): "+c+" | input codon nucleotides: "+inputCodons.get(c)+" | input codon translation: "+Utils.trans(inputCodons.get(c)), Borders.getFont(Borders.GENOWINFONT)));
            }
            int blankNo = 36 - inputCodons.size();
            for(int c=0; c<blankNo; c++){theCodonButtons.add(new JPanel());}
            cPanelInner.add(theCodonButtons, "Table"+index);
            layout.show(cPanelInner, "Table"+index);
            index++;
        }
    }

    private boolean isInputOK(){
        if(text.getText().length() > 108){
            JOptionPane.showMessageDialog(null, "Region for genotyping must be less than or equal to 108 bases.", "alert", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        else if(text.getText().length() < 50){
            JOptionPane.showMessageDialog(null, "Region for genotyping must be greater than 50 bases.", "alert", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    @Override
    public void insertUpdate(DocumentEvent de) {setUpButtons();}

    @Override
    public void removeUpdate(DocumentEvent de) {setUpButtons();}

    @Override
    public void changedUpdate(DocumentEvent de) {}
}